package com.jd.mysql.mgr.controller.mgrinspectreport;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jd.mysql.mgr.pojo.MgrInspectReport;
import com.jd.mysql.mgr.service.IMgrInspectReportService;
import com.jd.mysql.mgr.util.DateUtil;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;

public class MgrInspectReportController extends MultiActionController {
	private IMgrInspectReportService mgrInspectReportService;

	public void setMgrInspectReportService(
			IMgrInspectReportService mgrInspectReportService) {
		this.mgrInspectReportService = mgrInspectReportService;
	}
	public ModelAndView reportlist(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String groupName = (String)request.getSession().getAttribute("groupname");
		if(!"mysql".equals(groupName)){
			HttpSession session = request.getSession(true);
			String username = (String) session.getAttribute("userName");
			request.setAttribute("msg", "Hi "+username+",welcome to 360buy.com database management system!");
			return new ModelAndView("welcome");
		}
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, 22);
		List<MgrInspectReport> list = mgrInspectReportService.getMgrInspectReportList(null, 22, start);
		Long totalCount = mgrInspectReportService.getMgrInspectReportListCount(null);
		map.put("totalCount", totalCount);
		map.put("dtoList",list);
		return new ModelAndView("inspectreport/reportList","map",map);
	}
	
	public void export(HttpServletRequest request,HttpServletResponse response) throws Exception {
		response.reset();
		response.setContentType("application/msexcel");
		response.setHeader("Content-Disposition", "inline;filename=InspectReport.xls");
		Workbook wb = null;
		OutputStream os = null;
		try{
			os = response.getOutputStream();
			wb = new HSSFWorkbook();
			Sheet sheet = wb.createSheet("Inspect Report");
			Row row = sheet.createRow((short)0);
			String[]  titles={"IP","TOP_CPU","AVG_CPU","TOP_MEM","AVG_MEM","IOPS","IOPF","PRW","DISK_USED","DISK_TOTAL","DATA_LENGTH","DATE_INCREMENT"};
			for(int i=0;i<titles.length;++i){
				row.createCell(i,CellStyle.ALIGN_CENTER).setCellValue(titles[i]);
			}
			String createdate = (String)request.getAttribute("createdate");
			Date date = DateUtil.getFormatedDate(createdate);
			List<MgrInspectReport> list = mgrInspectReportService.getMgrInspectReportList(date, GlobalStaticVariables.limitless, 0);
			for(int i=1;i<=list.size();i++){
				Row rowcontent = sheet.createRow(i);
				rowcontent.createCell(0,CellStyle.ALIGN_CENTER).setCellValue(list.get(0).getIp());
				rowcontent.createCell(1,CellStyle.ALIGN_CENTER).setCellValue(list.get(1).getTopCpu());
				rowcontent.createCell(2,CellStyle.ALIGN_CENTER).setCellValue(list.get(2).getAvgCpu());
				rowcontent.createCell(3,CellStyle.ALIGN_CENTER).setCellValue(list.get(3).getTopMem());
				rowcontent.createCell(4,CellStyle.ALIGN_CENTER).setCellValue(list.get(4).getAvgMem());
				rowcontent.createCell(5,CellStyle.ALIGN_CENTER).setCellValue(list.get(5).getIops());
				rowcontent.createCell(6,CellStyle.ALIGN_CENTER).setCellValue(list.get(6).getIopf());
				rowcontent.createCell(7,CellStyle.ALIGN_CENTER).setCellValue(list.get(7).getPrw());
				rowcontent.createCell(8,CellStyle.ALIGN_CENTER).setCellValue(list.get(8).getDiskUsed());
				rowcontent.createCell(9,CellStyle.ALIGN_CENTER).setCellValue(list.get(9).getDiskTotal());
				rowcontent.createCell(10,CellStyle.ALIGN_CENTER).setCellValue(list.get(10).getDataLength());
				rowcontent.createCell(11,CellStyle.ALIGN_CENTER).setCellValue(list.get(11).getDataIncreament());
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(wb!=null){
				wb.write(os);
			}
			if(os!=null){
				os.flush();
				os.close();
			}
		}
	}
}
